﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using CoolERP_BLL;
using CoolERP_Entities;
using System.Text;

namespace CoolERP_Web
{
    public class GenYWLSH
    {
        /// <summary>
        /// 5 + 分厂号 + 业务类型号 + 日期 + 流水号
        /// </summary>
        /// <param name="fcbh"></param>
        /// <param name="ywlxbh"></param>
        /// <returns></returns>
        public static string GenBH(string ywlxbh, out ZL_YWLSH entity, out bool isAdd)
        {
            StringBuilder sb = new StringBuilder();
            string fcbh = Utility.FactoryBH;//分厂号
            int zdlsh = 1;//最大流水号
            sb.Append("5");
            sb.Append(fcbh);
            sb.Append(ywlxbh);//业务类型号
            sb.Append("-");//分隔符
            sb.Append(DateTime.Now.ToString("yyyyMMdd"));//日期
            sb.Append("-");//分隔符
            using (ZL_YWLSHBLL zBLL = new ZL_YWLSHBLL())
            {
                DateTime dt = DateTime.ParseExact(DateTime.Now.ToString("yyyyMMdd"), "yyyyMMdd", System.Threading.Thread.CurrentThread.CurrentCulture);
                entity = zBLL.GetEntity(t => t.FCBH == fcbh && t.RQ.Date == dt.Date && t.YWLXBH == ywlxbh);
                if (entity == null)
                {
                    entity = new ZL_YWLSH();
                    entity.ID = Guid.NewGuid();
                    entity.FCBH = fcbh;
                    entity.YWLXBH = ywlxbh;
                    entity.RQ = dt;
                    entity.ZDLSH = zdlsh;
                    isAdd = true;
                    //zBLL.Insert(entity);
                }
                else
                {
                    entity.ZDLSH++;
                    zdlsh = entity.ZDLSH;
                    isAdd = false;
                    //zBLL.Update(entity);
                }
            }
            sb.Append(zdlsh.ToString().PadLeft(4, '0'));
            return sb.ToString();
        }
    }
}